home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 3: The Continuation / 17-Bit_The_Continuation_Disc.iso / amigan / amigan 3 / amiga / ckiutl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-27  |  11.6 KB  |  535 lines

  1. /*  C K I U T L -- July 17, 1986
  2.  * Utility functions for C-Kermit on the Amiga
  3.  */
  4.  
  5. /*
  6.  Author: Jack Rouse
  7.  Contributed to Columbia University for inclusion in C-Kermit.
  8.  Copyright (C) 1986, Jack J. Rouse, 106 Rubin Ct. Apt. A-4, Cary NC 27511
  9.  Permission is granted to any individual or institution to use, copy, or
  10.  redistribute this software so long as it is not sold for profit, provided this
  11.  copyright notice is retained. 
  12.  
  13.  The file status routines assume all file protection modes are real, instead
  14.  of just delete protection on files and write protection on disks.
  15. */
  16.  
  17. #include <stdio.h>
  18. #undef NULL
  19. #include "exec/types.h"
  20. #include "exec/exec.h"
  21. #include "libraries/dos.h"
  22. #include "libraries/dosextens.h"
  23. #define fh_Interact fh_Port
  24. #define fh_Process  fh_Type
  25. #define ACTION_CLOSE 1007
  26. #ifdef LAT304
  27. #include "lattice/fcntl.h"
  28. #include "lattice/signal.h"
  29. #else
  30. #include "lattice/ios1.h"    /* defines ufbs structure */
  31. #endif
  32.  
  33. /* external routine definitions */
  34. APTR AllocMem();
  35. LONG AllocSignal();
  36. struct IORequest *CheckIO();
  37. VOID CloseDevice();
  38. VOID CloseLibrary();
  39. LONG DoIO();
  40. struct MsgPort *FindPort();
  41. struct Task *FindTask();
  42. VOID FreeMem();
  43. VOID FreeSignal();
  44. struct Message *GetMsg();
  45. LONG OpenDevice();
  46. struct Library *OpenLibrary();
  47. VOID PutMsg();
  48. VOID ReplyMsg();
  49. VOID SendIO();
  50. LONG SetSignal();
  51. VOID Signal();
  52. LONG Wait();
  53. LONG WaitIO();
  54. struct Message *WaitPort();
  55.  
  56. struct IORequest *CreateExtIO();
  57. struct MsgPort *CreatePort();
  58.  
  59. VOID Close();
  60. LONG DeleteFile();
  61. LONG Execute();
  62. BPTR Input(), Output();
  63. LONG IoErr();
  64. LONG IsInteractive();
  65. BPTR Lock();
  66. BPTR Open();
  67. LONG Read();
  68. VOID UnLock();   
  69. LONG WaitForChar();
  70.  
  71. /* portable library */
  72. char *malloc();
  73.  
  74. #ifdef LAT304
  75. /* translate Unix file handle (0, 1, or 2) to AmigaDOS file handle */
  76. #define DOSFH(n) fileno(&_iob[n])
  77. /* translate Unix file handle (0, 1, or 2) to Lattice file handle */
  78. #define FILENO(n) fileno(&_iob[n])
  79. #else
  80. /* Lattice runtime externals */
  81. extern struct UFB _ufbs[];
  82. extern int Enable_Abort;
  83. #define DOSFH(n) (_ufbs[n].ufbfh)
  84. #define FILENO(n) (n)
  85. #endif
  86.  
  87. /* Amiga Kermit externals (defined in ckitio.c) */
  88. extern struct Process *CurProc;
  89. extern struct CommandLineInterface *CurCLI;
  90.  
  91. /*
  92.  * CreatePacket -- allocate and set up a AmigaDOS packet
  93.  */
  94. struct DosPacket *CreatePacket()
  95. {
  96.     register struct StandardPacket *sp;
  97.  
  98.     sp = (struct StandardPacket *)
  99.          AllocMem((LONG)sizeof(struct StandardPacket),
  100.               (LONG)MEMF_PUBLIC|MEMF_CLEAR);
  101.     if (sp == NULL) return(NULL);
  102.     sp->sp_Pkt.dp_Link = &sp->sp_Msg;
  103.     sp->sp_Msg.mn_Node.ln_Type = NT_MESSAGE;
  104.     sp->sp_Msg.mn_Node.ln_Name = (char *)&sp->sp_Pkt;
  105.     sp->sp_Msg.mn_Length = sizeof(struct DosPacket);
  106.     return(&sp->sp_Pkt);
  107. }
  108.  
  109. /*
  110.  * DeletePacket -- deallocate packet from CreatePacket()
  111.  */
  112. VOID DeletePacket(pkt)
  113. struct DosPacket *pkt;
  114. {
  115.     FreeMem(pkt->dp_Link, (LONG)sizeof(struct StandardPacket));
  116. }
  117.  
  118. /*
  119.  * system(cmd) -- execute a command
  120.  *    provides no sensible return value
  121.  */
  122. system(cmd)
  123. char *cmd;
  124. {
  125.     BPTR fh;
  126.  
  127.     fflush(stdout);
  128.     if (cmd == NULL || *cmd == '\0')
  129.     {
  130.         fh = Open("CON:0/0/640/200/Kermit CLI", (LONG)MODE_NEWFILE);
  131.         if (fh)
  132.         {
  133.             Execute("", fh, (BPTR)NULL);
  134.  
  135.             /* fix 'endcli' bug */
  136.             ((struct FileHandle *)BADDR(fh))->fh_Pos = 0;
  137.  
  138.             Close(fh);
  139.         }
  140.     }
  141.     else
  142.         Execute(cmd, (BPTR)NULL, DOSFH(1));
  143. }
  144.  
  145. #ifndef LAT304
  146. /*
  147.  * getcwd -- get current working directory text
  148.  */
  149. char *getcwd(buf, len)
  150. register char *buf;
  151. int len;
  152. {
  153.     register UBYTE *dirname;
  154.  
  155.     if (CurCLI == NULL) return(NULL);
  156.     dirname = (UBYTE *)BADDR(CurCLI->cli_SetName);
  157.     if (len < *dirname + 1) return(NULL);
  158.     strncpy(buf, dirname + 1, *dirname);
  159.     buf[*dirname] = 0;
  160.     return(buf);
  161. }
  162.  
  163. /*
  164.  * update the current directory name in the CLI process structure
  165.  *
  166.  * This version generates the new name from the current name and
  167.  * the specified path, something like the CD command does.
  168.  * A much better version could be written using Parent(), Examine(),
  169.  * and VolInfo to reconstitute a name from a lock, but Parent() doesn't
  170.  * work with RAM: in V1.1.  The current implementation, like the CD
  171.  * command, has difficulty with backing up the directory tree.  For example
  172.  * { chdir("c:"); chdir("/"); } results in a dir name of "c:/".  However,
  173.  * this version shouldn't get as far out of sync as the CD command.  (The
  174.  * same sequence with the CD command puts you in the parent dir of "c:" but
  175.  * with a directory name of "c:".)
  176.  */
  177. static void update_dirname(name)
  178. register char *name;
  179. {
  180.     register UBYTE *dirname;        /* DOS directory name BSTR */
  181.     char buf[100];                /* about same size as DOS */
  182.     register char *tail;
  183.  
  184.     /* locate the DOS copy of the directory name */
  185.     if (CurCLI == NULL) return;
  186.     dirname = (UBYTE *)BADDR(CurCLI->cli_SetName);
  187.  
  188.     /* if the name is anchored (like "DF1:") simply replace the name */
  189.     if (strrchr(name, ':') != NULL)
  190.     {
  191.         *dirname = strlen(name);
  192.         strncpy(&dirname[1], name, *dirname);
  193.         return;
  194.     }
  195.  
  196.     /* name is relative to current directory, copy name to work with */
  197.     strncpy(buf, &dirname[1], *dirname);
  198.     tail = &buf[*dirname];
  199.  
  200.     /* traverse the path in the name */
  201.     while (*name)
  202.     {
  203.         /* go to parent dir? */
  204.         if (*name == '/')
  205.         {    /* remove a component from the directory path */
  206.  
  207.             /* advance past parent slash */
  208.             ++name;
  209.  
  210.             /* if at colon, can't back up */
  211.             if (tail[-1] == ':')
  212.             {
  213.                 *tail++ = '/';
  214.                 continue;
  215.             }
  216.  
  217.             /* if at slash, see if name given */
  218.             if (tail[-1] == '/')
  219.             {
  220.                 /* if no name, can't back up */
  221.                 if (tail[-2] == '/' || tail[-2] == ':')
  222.                 {
  223.                     *tail++ = '/';
  224.                     continue;
  225.                 }
  226.  
  227.                 /* remove trailing slash */
  228.                 --tail;
  229.             }
  230.  
  231.             /* remove remainder of component */
  232.             while (tail[-1] != '/' && tail[-1] != ':')
  233.                 --tail;
  234.         }
  235.         else
  236.         {    /* add component to directory path */
  237.             /* add slash if necessary to separate name */
  238.             if (tail[-1] != ':' && tail[-1] != '/')
  239.                 *tail++ = '/';
  240.  
  241.             /* add component name */
  242.             while (*name && *name != '/')
  243.                 *tail++ = *name++;
  244.  
  245.             /* add trailing slash if specified */
  246.             if (*name == '/')
  247.                 *tail++ = *name++;
  248.         }
  249.     }
  250.  
  251.     /* set BSTR to derived name */
  252.     *dirname = tail - buf;
  253.     strncpy(&dirname[1], buf, *dirname);
  254. }
  255.  
  256. /*
  257.  * change current directory
  258.  */
  259. int chdir(name)
  260. char *name;
  261. {
  262.     BPTR lock;
  263.     BPTR oldlock;
  264.     struct FileInfoBlock *fib;
  265.  
  266.     /* ignore chdir("") */
  267.     if (*name == 0) return(0);
  268.  
  269.     /* try to look at the object */ 
  270.     lock = Lock(name, (LONG)ACCESS_READ);
  271.     if (lock == NULL) return(-1);
  272.  
  273.     /* make sure the file is a directory */
  274.     fib = (struct FileInfoBlock *)malloc(sizeof(*fib));
  275.     if (fib == NULL || !Examine(lock, fib) || fib->fib_DirEntryType <= 0)
  276.     {
  277.         if (fib) free(fib);
  278.         UnLock(lock);
  279.         return(-1);
  280.     }
  281.  
  282.     /* don't need file info any more */
  283.     free(fib);
  284.  
  285.     /* change the current directory */
  286.     oldlock = CurrentDir(lock);
  287.  
  288.     /* update the DOS copy of the directory name */
  289.     update_dirname(name);
  290.  
  291.     /* unlock the previous current directory */
  292.     if (oldlock) UnLock(oldlock);
  293.  
  294.     return(0);
  295. }
  296.  
  297. /*
  298.  * print an error message with explanation
  299.  * (no explanation currently)
  300.  */
  301. int perror(str)
  302. char *str;
  303. {
  304.     extern int errno;
  305.     LONG oserr = IoErr();
  306.  
  307.     printf("%s: errno=%d, oserr=%ld\n", str, errno, oserr);
  308. }
  309. #endif
  310.  
  311. /*
  312.  * isatty(fd) -- determine if given file is a (virtual) terminal
  313.  *    only works for fd=0, 1, or 2 (stdin, stdout, and stderr resp.)
  314.  */ 
  315. int isatty(fd)
  316. int fd;
  317. {
  318.     return(IsInteractive(DOSFH(fd)) != 0);
  319. }
  320.  
  321. /*
  322.  * readstat -- determine file's read status
  323.  *    returns -3 if file read protected
  324.  *    returns -2 if file is a directory
  325.  *    returns -1 if file doesn't exist
  326.  *    returns file size otherwise
  327.  */
  328. long readstat(name)
  329. char *name;
  330. {
  331.     BPTR lock;
  332.     struct FileInfoBlock *fib;
  333.     long size;
  334.  
  335.     /* locate the file */ 
  336.     if ((lock = Lock(name, (LONG)ACCESS_READ)) == NULL) return(-1);
  337.  
  338.     /* allocate a file info block */
  339.     if ((fib = (struct FileInfoBlock *)malloc(sizeof(*fib))) == NULL)
  340.     {    size = -1; goto exit; }
  341.  
  342.     /* make sure it's not a directory */
  343.     if (!Examine(lock, fib) || fib->fib_DirEntryType >= 0)
  344.     {    size = -2; goto exit; }
  345.  
  346.     /* make sure it's readable */
  347.     if (fib->fib_Protection & FIBF_READ)
  348.     {    size = -3; goto exit; }
  349.  
  350.     size = fib->fib_Size;
  351. exit:
  352.     if (fib) free(fib);
  353.     UnLock(lock);
  354.     return(size);
  355. }
  356.  
  357. /*
  358.  * writestat -- determines file's write status
  359.  *    returns 0 if file should be writable, -1 otherwise
  360.  *
  361.  * The following logic is used to determine if a file is writable:
  362.  *    1.  If the file exists, it must not be write or delete protected.
  363.  *        (Delete protection subsumes write protection for overwriting.)
  364.  *    2.  The parent directory must not be write protected ???
  365.  *    3.  The volume that the parent directory resides on must be
  366.  *        validated and not write protected.
  367.  */
  368. int writestat(name)
  369. register char *name;
  370. {
  371.     register char *p;
  372.     char *lastslash;
  373.     char path[100];
  374.     BPTR lock;
  375.     struct FileInfoBlock *fib;
  376.     struct InfoData *id = NULL;
  377.     int rc = -1;
  378.  
  379.     /* allocate a FileInfoBlock */
  380.     fib = (struct FileInfoBlock *)malloc(sizeof(*fib));
  381.     if (fib == NULL) goto exit;
  382.  
  383.     /* see if the file exists */
  384.     if ((lock = Lock(name, (LONG)ACCESS_READ)) != NULL)
  385.     {
  386.         /* make sure it's not a directory or write protected */
  387.         if (!Examine(lock, fib) ||
  388.             fib->fib_DirEntryType >= 0 ||
  389.             (fib->fib_Protection & (FIBF_WRITE|FIBF_DELETE)) )
  390.             goto exit;
  391.         UnLock(lock);
  392.     }
  393.  
  394.     /* strip path from name */
  395.     lastslash = NULL;
  396.     for (p = path; *p = *name; ++p, ++name)
  397.         if (*p == '/' || *p == ':')
  398.             lastslash = p;
  399.  
  400.     /* make sure the path exists */
  401.     if (lastslash)
  402.     {
  403.         lastslash[1] = 0;
  404.         lock = Lock(path, (LONG)ACCESS_READ);
  405.     }
  406.     else
  407.         lock = (CurProc->pr_CurrentDir)
  408.                ? DupLock(CurProc->pr_CurrentDir)
  409.                : NULL;
  410.  
  411.     /* make sure it is a directory that is not write protected */
  412.     if (lock == NULL || !Examine(lock, fib) ||
  413.         fib->fib_DirEntryType <= 0 ||
  414.         (fib->fib_Protection & FIBF_WRITE) )
  415.         goto exit;
  416.  
  417.     /* get device info */    
  418.     if ((id = (struct InfoData *)malloc(sizeof(*id))) == NULL) goto exit;
  419.  
  420.     /* make sure the disk is writeable */
  421.     if (Info(lock, id) && id->id_DiskState == ID_VALIDATED)
  422.         rc = 0;
  423.  
  424.     /* clean up */
  425. exit:
  426.     if (id) free(id);
  427.     if (fib) free(fib);
  428.     if (lock) UnLock(lock);
  429.         return(rc);
  430. }
  431.  
  432. /*
  433.  * pipeopen -- execute command to read output like a file
  434.  */
  435. #define PIPEHOLDER "RAM:Pipe-Holder"
  436. FILE *pipeopen(command)
  437. char *command;
  438. {
  439.     BPTR fh;
  440.  
  441.     /* create holder file */
  442.     fh = Open(PIPEHOLDER, (LONG)MODE_NEWFILE);
  443.     if (fh == NULL) return(0);
  444.  
  445.     /* execute the command */
  446.     Execute(command, (BPTR)NULL, fh);
  447.  
  448.     /* close the holder file */
  449.     Close(fh);
  450.  
  451.     /* reopen it for input */
  452.     return(fopen(PIPEHOLDER, "r"));
  453. }
  454.  
  455. /*
  456.  * pipeclose -- clean up after pipe open
  457.  */
  458. pipeclose(f)
  459. FILE *f;
  460. {
  461.     fclose(f);
  462.     DeleteFile(PIPEHOLDER);
  463. }
  464.  
  465. /*
  466.  * existobj -- return true if file system object exists
  467.  */
  468. int existobj(name)
  469. char *name;
  470. {
  471.     BPTR lock;
  472.  
  473.     if (*name == 0) return(CurProc->pr_CurrentDir != NULL);
  474.     if ((lock = Lock(name, (LONG)ACCESS_READ)) == NULL)
  475.         return(0);
  476.     UnLock(lock);
  477.     return(1);
  478. }
  479.  
  480. /* opendir handle structure */
  481. struct DirHandle {
  482.     struct FileInfoBlock fib;
  483.     BPTR lock;
  484. };
  485.  
  486. /*
  487.  * attempt to open a directory, fill in a handle structure
  488.  */
  489. struct DirHandle *opendir(name)
  490. char *name;
  491. {
  492.     BPTR lock;
  493.     struct DirHandle *dh;
  494.  
  495.     /* get lock on name ("" is current dir) */
  496.     lock = (*name != 0)        ? Lock(name, (LONG)ACCESS_READ) :
  497.            (CurProc->pr_CurrentDir) ? DupLock(CurProc->pr_CurrentDir)
  498.                     : NULL;
  499.     if (lock == NULL) return(NULL);
  500.     dh = (struct DirHandle *)malloc(sizeof(*dh));
  501.     if (dh == NULL)
  502.     {
  503.         UnLock(lock);
  504.         return(NULL);
  505.     }
  506.     if (!Examine(lock, &dh->fib) || dh->fib.fib_DirEntryType <= 0)
  507.     {
  508.         UnLock(lock);
  509.         free(dh);
  510.         return(NULL);
  511.     }
  512.     /* pass it into the caller's care */
  513.     dh->lock = lock;
  514.     return(dh);
  515. }
  516.  
  517. /*
  518.  * return name for next entry in dir
  519.  */
  520. char *readdir(dh)
  521. struct DirHandle *dh;
  522. {
  523.     return(ExNext(dh->lock, &dh->fib) ? dh->fib.fib_FileName : NULL);
  524. }
  525.  
  526. /*
  527.  * finish accessing a directory
  528.  */
  529. VOID closedir(dh)
  530. struct DirHandle *dh;
  531. {
  532.     UnLock(dh->lock);
  533.     free(dh);
  534. }
  535.